package com.mb.app.appused.views;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ListMenu {
	ExpandableListView view;
	Context context;
	public MenuAdapter adapter;
	MenuSelectListener menuListener;

	public int groupLayout = -1;
	public int groupIconId = -1;
	public int groupTxtId = -1;

	public int itemLayout = -1;
	public int itemIconId = -1;
	public int itemTxtId = -1;

	public ListMenu(Context context, final ExpandableListView view) {
		this.view = view;
		this.context = context;
		adapter = new MenuAdapter(this.context);
		this.view.setAdapter(adapter);
		view.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
					long id) {
				// TODO Auto-generated method stub

				adapter.notifyDataSetChanged();
				if (menuListener != null) {
					menuListener.OnSelect(groupPosition, childPosition);
					if (adapter.getSelectChild() != childPosition || adapter.getSelectGrop() != groupPosition) {
						menuListener.OnSelectChange(groupPosition, childPosition);
					}
				}

				adapter.setSelectItem(groupPosition, childPosition);
				return false;
			}
		});

		view.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				// TODO Auto-generated method stub

				if (adapter.getChildrenCount(groupPosition) < 1) {

					if (menuListener != null) {
						menuListener.OnSelect(groupPosition, 0);
						if (adapter.getSelectGrop() != groupPosition) {
							menuListener.OnSelectChange(groupPosition, 0);
						}
					}
				}

				if (menuListener != null) {
					menuListener.OnGroupSelect(groupPosition);
				}
				return false;
			}
		});

		view.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				adapter.setOpen(groupPosition, view);
				adapter.notifyDataSetChanged();
			}
		});
	}

	/**
	 * 设置监听动作
	 * 
	 * @param listener
	 */
	public void setMenuListener(MenuSelectListener listener) {
		this.menuListener = listener;
	}

	/**
	 * 添加组
	 * 
	 * @param icon
	 * @param name
	 */
	public void addGroup(int icon, String name) {
		adapter.addGroup(icon, name);
	}

	/**
	 * 添加子菜单
	 * 
	 * @param icon
	 * @param name
	 * @param group
	 */
	public void addChild(int icon, String name, IconMenuGroup group) {
		group.addChild(icon, name);
	}
	/**
	 * 获取适配器
	 * @return
	 */
	public MenuAdapter getAdapter() {
		return adapter;
	}
	/**
	 * 获取父布局 layout
	 * @return
	 */
	public int getGroupLayout() {
		return groupLayout;
	}
	/**
	 * 设置父布局 layout
	 * @param groupLayout
	 */
	public void setGroupLayout(int groupLayout) {
		this.groupLayout = groupLayout;
	}
	/**
	 * 获取 父菜单 icon id
	 * @return
	 */
	public int getGroupIconId() {
		return groupIconId;
	}
	/**
	 * 设置 父菜单 icon id  无图标-1
	 * @param groupIconId
	 */
	public void setGroupIconId(int groupIconId) {
		this.groupIconId = groupIconId;
	}
	/**
	 * 父菜单 文本ID
	 * @return
	 */
	public int getGroupTxtId() {
		return groupTxtId;
	}
	/**
	 * 父菜单 文本ID
	 * @param groupTxtId
	 */
	public void setGroupTxtId(int groupTxtId) {
		this.groupTxtId = groupTxtId;
	}
	/**
	 * 子菜单 布局
	 * @return
	 */
	public int getItemLayout() {
		return itemLayout;
	}
	/**
	 * 子菜单布局
	 * @param itemLayout
	 */
	public void setItemLayout(int itemLayout) {
		this.itemLayout = itemLayout;
	}
	/**
	 * 子菜单图标  无图标-1
	 * @return
	 */
	public int getItemIconId() {
		return itemIconId;
	}
	/**
	 * 子菜单图标
	 * @param itemIconId
	 */
	public void setItemIconId(int itemIconId) {
		this.itemIconId = itemIconId;
	}
	/**
	 * 子菜单文本
	 * @return
	 */
	public int getItemTxtId() {
		return itemTxtId;
	}
	/**
	 * 子菜单文本
	 * @param itemTxtId
	 */
	public void setItemTxtId(int itemTxtId) {
		this.itemTxtId = itemTxtId;
	}

	class MenuAdapter extends BaseExpandableListAdapter {
		Context context;
		List<IconMenuGroup> iconMenu = new ArrayList<ListMenu.IconMenuGroup>();

		int select = 0;
		int selectChild = 0;
		int selectGrop = 0;

		MenuAdapter(Context context) {
			this.context = context;
		}

		/**
		 * 添加组
		 * 
		 * @param icon
		 * @param name
		 */
		public void addGroup(int icon, String name) {
			IconMenuGroup temp = new IconMenuGroup(icon, name);
			iconMenu.add(temp);
		}

		/**
		 * 添加子菜单
		 * 
		 * @param icon
		 * @param name
		 * @param group
		 */
		public void addChild(int icon, String name, IconMenuGroup group) {
			group.addChild(icon, name);
		}

		public int getSelectChild() {
			return selectChild;
		}

		public void setSelectChild(int selectChild) {
			this.selectChild = selectChild;
		}

		public int getSelectGrop() {
			return selectGrop;
		}

		public void setSelectGrop(int selectGrop) {
			this.selectGrop = selectGrop;
		}

		public void setSelectItem(int group, int child) {
			selectChild = child;
			selectGrop = group;
		}

		public void setOpen(int i, ExpandableListView list) {
			select = i;
			for (int temp = 0; temp < iconMenu.size(); temp++) {
				if (temp == i) {

				} else {
					list.collapseGroup(temp);
				}
			}
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return iconMenu.get(groupPosition).getChildSize();
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			MenuHolder itemHolder = null;
			if (itemLayout == -1)
				return null;
			if (convertView == null) {
				convertView = (View) LayoutInflater.from(context).inflate(itemLayout, null);
				itemHolder = new MenuHolder();
				itemHolder.txt = (TextView) convertView.findViewById(itemTxtId);
				itemHolder.icon = (ImageView) convertView.findViewById(itemIconId);
				convertView.setTag(itemHolder);
			} else {
				itemHolder = (MenuHolder) convertView.getTag();
			}
			itemHolder.txt.setText(iconMenu.get(groupPosition).getChildName(childPosition));
			if (groupPosition == selectGrop && childPosition == selectChild) {
				itemHolder.txt.setTextColor(Color.rgb(238, 238, 238));
			} else {
				itemHolder.txt.setTextColor(Color.rgb(79, 79, 79));
			}

			if (iconMenu.get(groupPosition).getChildIcon(childPosition) != -1) {
				itemHolder.icon.setVisibility(View.VISIBLE);
				itemHolder.icon.setImageResource(iconMenu.get(groupPosition).getChildIcon(childPosition));
			} else {
				itemHolder.icon.setVisibility(View.INVISIBLE);
			}

			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return iconMenu.get(groupPosition).getChildSize();
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return iconMenu.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return iconMenu.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			MenuHolder groupHolder = null;

			if (groupLayout == -1)
				return null;

			if (convertView == null) {
				convertView = (View) LayoutInflater.from(context).inflate(groupLayout, null);
				groupHolder = new MenuHolder();
				groupHolder.txt = (TextView) convertView.findViewById(groupTxtId);
				groupHolder.icon = (ImageView) convertView.findViewById(groupIconId);
				convertView.setTag(groupHolder);
			} else {
				groupHolder = (MenuHolder) convertView.getTag();
			}
			groupHolder.txt.setText(iconMenu.get(groupPosition).getGroupName());

			if (iconMenu.get(groupPosition).getGroupIcon() != -1) {
				groupHolder.icon.setVisibility(View.VISIBLE);
				groupHolder.icon.setImageResource(iconMenu.get(groupPosition).getGroupIcon());
			} else {
				groupHolder.icon.setVisibility(View.INVISIBLE);
			}

			if (groupPosition == select) {
				groupHolder.txt.setTextColor(Color.rgb(238, 238, 238));
			} else {
				groupHolder.txt.setTextColor(Color.rgb(79, 79, 79));
			}
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
	}

	/**
	 * 一级菜单
	 * 
	 * @author xhwwzd
	 *
	 */
	static class MenuHolder {
		public ImageView icon;
		public TextView txt;
	}

	/**
	 * 带图标
	 * 
	 * @author xhwwzd
	 *
	 */
	class IconMenuGroup {
		public MenuItem groupItem = new MenuItem();
		public List<MenuItem> itemGroup = new ArrayList<ListMenu.MenuItem>();

		public IconMenuGroup(String name) {
			groupItem.setName(name);
		}

		public IconMenuGroup(int icon, String name) {
			groupItem.setName(name);
			groupItem.setIcon(icon);
		}

		public String getGroupName() {
			return groupItem.name;
		}

		public int getGroupIcon() {
			return groupItem.icon;
		}

		public int getChildSize() {
			return itemGroup.size();
		}

		public MenuItem getChild(int item) {
			return itemGroup.get(item);
		}

		public String getChildName(int item) {
			return getChild(item).name;
		}

		public int getChildIcon(int item) {
			return getChild(item).icon;
		}

		public void addChild(int icon, String txt) {
			MenuItem item = new MenuItem();
			item.setIcon(icon);
			item.setName(txt);
			itemGroup.add(item);
		}

	}

	class MenuItem {
		public int icon = -1;
		public String name;

		public int getIcon() {
			return icon;
		}

		public void setIcon(int icon) {
			this.icon = icon;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

	/**
	 * 监听菜单动作
	 * 
	 * @author xhwwzd
	 *
	 */
	interface MenuSelectListener {
		void OnSelect(int group, int child);

		void OnSelectChange(int group, int child);

		void OnGroupSelect(int group);

	}
}
